package com.liser.southernstar.collection.ui.fragment;

import android.view.View;
import android.widget.LinearLayout;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.liser.southernstar.R;
import com.liser.southernstar.framework.ui.BaseFragment;
import com.liser.southernstar.neleme.adapters.CarAdapter;
import com.liser.southernstar.neleme.bean.FoodBean;
import com.liser.southernstar.neleme.utils.ViewUtils;
import com.liser.southernstar.neleme.views.AddWidget;
import com.liser.southernstar.neleme.views.ListContainerView;
import com.liser.southernstar.neleme.views.ShopCarView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * @author LISER
 */
public class OrderFragment extends BaseFragment {

    @BindView(R.id.menu_layout)
    CoordinatorLayout mMenuCoordinatorLayout;

    @BindView(R.id.list_container)
    ListContainerView listContainerView;

    @BindView(R.id.car_container)
    LinearLayout carContainer;

    @BindView(R.id.car_mainfl)
    ShopCarView shopCarView;

    @BindView(R.id.car_recyclerview)
    RecyclerView carRecView;

    @BindView(R.id.blackview)
    View blackView;

    public BottomSheetBehavior behavior;

    private CarAdapter carAdapter;

    private AddWidget.OnAddClick onAddClick = new AddWidget.OnAddClick() {
        @Override
        public void onAddClick(View view, FoodBean fb) {
            dealCar(fb);
            ViewUtils.addTvAnim(view, shopCarView.carLoc, getContext(), mMenuCoordinatorLayout);
        }

        @Override
        public void onSubClick(FoodBean fb) {
            dealCar(fb);
        }
    };

    @Override
    protected int onLayout() {
        return R.layout.fragment_order;
    }

    @Override
    protected void onView() {
        initShopCar();
    }

    private void initShopCar() {
        listContainerView.setAddClick(onAddClick);
        behavior = BottomSheetBehavior.from(carContainer);
        shopCarView.setBehavior(behavior, blackView);
//		carRecView.setNestedScrollingEnabled(false);
        carRecView.setLayoutManager(new LinearLayoutManager(getContext()));
        if (carRecView.getItemAnimator() != null) {
            ((DefaultItemAnimator) carRecView.getItemAnimator()).setSupportsChangeAnimations(false);
        }
        carAdapter = new CarAdapter(new ArrayList<>(), onAddClick);
        carAdapter.bindToRecyclerView(carRecView);
    }

    private void dealCar(FoodBean foodBean) {
        HashMap<String, Long> typeSelect = new HashMap<>();//更新左侧类别badge用
        BigDecimal amount = new BigDecimal(0.0);
        int total = 0;
        boolean hasFood = false;
        if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
            listContainerView.foodAdapter.notifyDataSetChanged();
        }
        List<FoodBean> flist = carAdapter.getData();
        int p = -1;
        for (int i = 0; i < flist.size(); i++) {
            FoodBean fb = flist.get(i);
            if (fb.getId() == foodBean.getId()) {
                fb = foodBean;
                hasFood = true;
                if (foodBean.getSelectCount() == 0) {
                    p = i;
                } else {
                    carAdapter.setData(i, foodBean);
                }
            }
            total += fb.getSelectCount();
            if (typeSelect.containsKey(fb.getType())) {
                typeSelect.put(fb.getType(), typeSelect.get(fb.getType()) + fb.getSelectCount());
            } else {
                typeSelect.put(fb.getType(), fb.getSelectCount());
            }
            amount = amount.add(fb.getPrice().multiply(BigDecimal.valueOf(fb.getSelectCount())));
        }
        if (p >= 0) {
            carAdapter.remove(p);
        } else if (!hasFood && foodBean.getSelectCount() > 0) {
            carAdapter.addData(foodBean);
            if (typeSelect.containsKey(foodBean.getType())) {
                typeSelect.put(foodBean.getType(), typeSelect.get(foodBean.getType()) + foodBean.getSelectCount());
            } else {
                typeSelect.put(foodBean.getType(), foodBean.getSelectCount());
            }
            amount = amount.add(foodBean.getPrice().multiply(BigDecimal.valueOf(foodBean.getSelectCount())));
            total += foodBean.getSelectCount();
        }
        shopCarView.showBadge(total);
        listContainerView.typeAdapter.updateBadge(typeSelect);
        shopCarView.updateAmount(amount);
    }

    public void clearCar(View view) {
        ViewUtils.showClearCar(getContext(), (dialog, which) -> clearCar());
    }

    private void clearCar() {
        List<FoodBean> flist = carAdapter.getData();
        for (int i = 0; i < flist.size(); i++) {
            FoodBean fb = flist.get(i);
            fb.setSelectCount(0);
        }
        carAdapter.setNewData(new ArrayList<>());
        listContainerView.foodAdapter.notifyDataSetChanged();
        shopCarView.showBadge(0);
        listContainerView.typeAdapter.updateBadge(new HashMap<>());
        shopCarView.updateAmount(new BigDecimal(0.0));
    }
}
